﻿Public Class Formulario
    Dim estado As String
    Dim data_maestros As DataTable
    Dim cod_clie, cod_vended, fecha_d, fecha_h As String
    Dim ncotiz As String
    Dim where As String

    Private Sub Formulario_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        If DBObt_Maestros() = True Then
            Cargo_Maestro()
        End If
        obtengo_tabla_articulos()

        setear_grid_global(grilla_maestro)
        carga_combo_global(cb_busqueda_cliente, "select COD_CLIENT,RAZON_SOCI from gva14", "RAZON_SOCI", "COD_CLIENT")
        carga_combo_global(cb_busqueda_vendedor, "select COD_VENDED,NOMBRE_VEN from gva23 WHERE INHABILITA = 0", "NOMBRE_VEN", "COD_VENDED")
        Me.CenterToScreen()

    End Sub
    Private Sub bt_imprimir_Click(sender As Object, e As EventArgs)
        Form_prueba_reporte.Show()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        imprime_cotizacion(7, "000200002662")
    End Sub

    Function DBObt_Maestros() As Boolean
        Dim Cmd As SqlClient.SqlCommand
        Dim Adp As SqlClient.SqlDataAdapter
        Dim ds As New DataTable
        Dim sbQuery As String


        ds.Clear()
        Conectar()
        Try

            sbQuery = "SELECT TOP(10) GVA08.N_COTIZ,GVA08.FECHA_ALTA,(GVA08.IMPORTE + GVA08.IMPORTE_IV) AS TOTAL_COTIZ," & _
                      "GVA14.CUIT,GVA14.RAZON_SOCI,GVA23.NOMBRE_VEN " & _
                      "FROM gva08 " & _
                      "JOIN GVA14 ON GVA08.COD_CLIENT = GVA14.COD_CLIENT " & _
                      "JOIN GVA01 ON GVA08.COND_VTA = GVA01.COND_VTA " & _
                      "JOIN GVA23 ON GVA08.COD_VENDED = GVA23.COD_VENDED " & _
                      "order by FECHA_ALTA desc "

            Cmd = New SqlClient.SqlCommand(sbQuery, cn)
            Adp = New SqlClient.SqlDataAdapter(Cmd)
            Adp.Fill(ds)
            data_maestros = ds
            If data_maestros.Rows.Count > 0 Then
                Return True
            Else
                Return False
            End If
        Catch ex As Exception
            Return False
        End Try
        'desconectar()

    End Function
    Function DBObt_Maestros_filtro(where As String) As Boolean
        Dim Cmd As SqlClient.SqlCommand
        Dim Adp As SqlClient.SqlDataAdapter
        Dim ds As New DataTable
        Dim sbQuery As String

        ds.Clear()
        Conectar()
        Try

            sbQuery = "SELECT TOP(10) GVA08.N_COTIZ,GVA08.FECHA_ALTA,(GVA08.IMPORTE + GVA08.IMPORTE_IV) AS TOTAL_COTIZ," & _
                      "GVA14.CUIT,GVA14.RAZON_SOCI,GVA23.NOMBRE_VEN " & _
                      "FROM gva08 " & _
                      "JOIN GVA14 ON GVA08.COD_CLIENT = GVA14.COD_CLIENT " & _
                      "JOIN GVA01 ON GVA08.COND_VTA = GVA01.COND_VTA " & _
                      "JOIN GVA23 ON GVA08.COD_VENDED = GVA23.COD_VENDED " & _
                      "where " & where & " "

            Cmd = New SqlClient.SqlCommand(sbQuery, cn)
            Adp = New SqlClient.SqlDataAdapter(Cmd)
            Adp.Fill(ds)
            data_maestros = ds
            If data_maestros.Rows.Count > 0 Then
                Return True
            Else
                Return False
            End If
        Catch ex As Exception
            Return False
        End Try
        'desconectar()

    End Function
    Function Cargo_Maestro()
        Dim i As Integer
        limpia_grilla(grilla_maestro)
        Try
            For i = 0 To data_maestros.Rows.Count - 1
                grilla_maestro.Rows.Add()
                grilla_maestro.Item(0, i).Value() = data_maestros.Rows(i).Item("N_COTIZ").ToString.Trim
                grilla_maestro.Item(1, i).Value() = data_maestros.Rows(i).Item("FECHA_ALTA").ToString.Trim
                grilla_maestro.Item(2, i).Value() = data_maestros.Rows(i).Item("TOTAL_COTIZ").ToString.Trim
                grilla_maestro.Item(3, i).Value() = data_maestros.Rows(i).Item("CUIT").ToString.Trim
                grilla_maestro.Item(4, i).Value() = data_maestros.Rows(i).Item("RAZON_SOCI").ToString.Trim
                grilla_maestro.Item(5, i).Value() = data_maestros.Rows(i).Item("NOMBRE_VEN").ToString.Trim
            Next
        Catch ex As Exception

        End Try
    End Function
    Function opciones_panel_busqueda()
        If ch_busqueda_cliente.Checked = True Then
            cb_busqueda_cliente.Enabled = True '' combos
            tx_busqueda_cotizacion.Enabled = False
        Else
            cb_busqueda_cliente.Enabled = False '' combos
        End If
        If ch_busqueda_vendedor.Checked = True Then
            cb_busqueda_vendedor.Enabled = True
            tx_busqueda_cotizacion.Enabled = False
        Else
            cb_busqueda_vendedor.Enabled = False
        End If
        If ch_busqueda_fecha.Checked = True Then
            dt_busqueda_desde.Enabled = True
            dt_busqueda_hasta.Enabled = True
            tx_busqueda_cotizacion.Enabled = False
        Else
            dt_busqueda_desde.Enabled = False
            dt_busqueda_hasta.Enabled = False
        End If
        If ch_busqueda_cotizacion.Checked = True Then
            cb_busqueda_cliente.Enabled = False '' combos
            cb_busqueda_vendedor.Enabled = False
            dt_busqueda_desde.Enabled = False
            dt_busqueda_hasta.Enabled = False
            tx_busqueda_cotizacion.Enabled = True
        Else
            tx_busqueda_cotizacion.Enabled = False
        End If
    End Function

    Private Sub ch_busqueda_vendedor_CheckedChanged(sender As Object, e As EventArgs) Handles ch_busqueda_vendedor.CheckedChanged
        If ch_busqueda_vendedor.CheckState = CheckState.Checked Then
            ch_busqueda_cotizacion.CheckState = CheckState.Unchecked
        End If
        opciones_panel_busqueda()
        valida_filtro()
    End Sub
    Private Sub ch_busqueda_cliente_CheckedChanged(sender As Object, e As EventArgs) Handles ch_busqueda_cliente.CheckedChanged
        If ch_busqueda_cliente.CheckState = CheckState.Checked Then
            '    ch_busqueda_vendedor.CheckState = CheckState.Unchecked
            '    ch_busqueda_fecha.CheckState = CheckState.Unchecked
            ch_busqueda_cotizacion.CheckState = CheckState.Unchecked
        End If
        opciones_panel_busqueda()
        valida_filtro()
    End Sub

    Private Sub ch_busqueda_fecha_CheckedChanged(sender As Object, e As EventArgs) Handles ch_busqueda_fecha.CheckedChanged
        If ch_busqueda_fecha.CheckState = CheckState.Checked Then
            ' ch_busqueda_vendedor.CheckState = CheckState.Unchecked
            ' ch_busqueda_cliente.CheckState = CheckState.Unchecked
            ch_busqueda_cotizacion.CheckState = CheckState.Unchecked
        End If
        opciones_panel_busqueda()
        valida_filtro()
    End Sub

    Private Sub ch_busqueda_cotizacion_CheckedChanged(sender As Object, e As EventArgs) Handles ch_busqueda_cotizacion.CheckedChanged
        If ch_busqueda_cotizacion.CheckState = CheckState.Checked Then
            ch_busqueda_vendedor.CheckState = CheckState.Unchecked
            ch_busqueda_cliente.CheckState = CheckState.Unchecked
            ch_busqueda_fecha.CheckState = CheckState.Unchecked
        End If
        opciones_panel_busqueda()
        valida_filtro()
    End Sub

    Private Sub bt_filtrar_Click(sender As Object, e As EventArgs) Handles bt_filtrar.Click
        filtro()
        If DBObt_Maestros_filtro(where) = True Then
            Cargo_Maestro()
        End If

    End Sub
    Function filtro()
        cod_clie = cb_busqueda_cliente.SelectedValue
        cod_vended = cb_busqueda_vendedor.SelectedValue
        fecha_d = dt_busqueda_desde.Value.Month.ToString + "/" + dt_busqueda_desde.Value.Day.ToString + "/" + dt_busqueda_desde.Value.Year.ToString
        fecha_h = dt_busqueda_hasta.Value.Month.ToString + "/" + dt_busqueda_hasta.Value.Day.ToString + "/" + dt_busqueda_hasta.Value.Year.ToString
        ncotiz = tx_busqueda_cotizacion.Text
        where = ""
        If ch_busqueda_cliente.Checked = True Then
            where = "GVA08.COD_CLIENT = '" & cod_clie & "'"
        End If
        If ch_busqueda_vendedor.Checked = True Then
            If where = "" Then
                where += " GVA08.COD_VENDED = '" & cod_vended & "'"
            Else
                where += " AND GVA08.COD_VENDED = '" & cod_vended & "'"
            End If
        End If
        If ch_busqueda_fecha.Checked = True Then
            If where = "" Then
                where += "GVA08.FECHA_ALTA BETWEEN  '" & fecha_d & "' AND '" & fecha_h & "'"
            Else
                where += " AND GVA08.FECHA_ALTA BETWEEN  '" & fecha_d & "' AND '" & fecha_h & "'"
            End If

        End If
        If ch_busqueda_cotizacion.Checked = True Then
            If where = "" Then
                where += "GVA08.N_COTIZ LIKE  ' %" & ncotiz & "%'"
            End If
        End If

    End Function
    Private Sub bt_nuevo_Click(sender As Object, e As EventArgs) Handles bt_nuevo.Click
        Form_alta_cot.ShowDialog()
    End Sub
    Private Sub bt_mostrar_todo_Click(sender As Object, e As EventArgs) Handles bt_mostrar_todo.Click
        If DBObt_Maestros() = True Then
            Cargo_Maestro()
        End If
    End Sub
    Function valida_filtro() As Boolean
        Dim retorna As Boolean
        If ch_busqueda_cliente.Checked = False And ch_busqueda_vendedor.Checked = False And ch_busqueda_fecha.Checked = False And ch_busqueda_cotizacion.Checked = False Then
            retorna = False
            If cb_busqueda_cliente.Enabled = True And cb_busqueda_cliente.SelectedIndex = 0 Then
                Dim er As ErrorProvider

            End If
        Else
            retorna = True
        End If


        If retorna = False Then
            bt_filtrar.Enabled = False
        Else
            bt_filtrar.Enabled = True
        End If
    End Function
End Class
